package com.bjtech.web.action.management;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ssi.web.struts.bean.UploadFile;

import com.bjtech.dao.BaseBuildingInfoDao;
import com.bjtech.dao.BaseRoomInfoDao;
import com.bjtech.dao.BaseShareAreaInfoDao;
import com.bjtech.dao.StandardSinkParamDao;
import com.bjtech.dao.SysAccessaryDao;
import com.bjtech.domain.BaseBuildingInfo;
import com.bjtech.domain.BaseShareAreaInfo;
import com.bjtech.domain.StandardSinkParam;
import com.bjtech.domain.SysAccessary;
import com.bjtech.domain.query.BaseBuildingInfoQuerySpec;
import com.bjtech.domain.query.BaseShareAreaInfoQuerySpec;
import com.bjtech.domain.query.StandardSinkParamQuerySpec;
import com.bjtech.web.action.LyheatBaseAction;

public class BaseBuildingInfoAction extends LyheatBaseAction {

	private static final long serialVersionUID = 1L;
	private BaseBuildingInfoDao baseBuildingInfoDao;
	private BaseShareAreaInfoDao baseShareAreaInfoDao;
	private SysAccessaryDao sysAccessaryDao;
	private BaseBuildingInfoQuerySpec selectBean;

	private List shareAreaList;
	private SysAccessary accessary;
	
	private List roomList;
	private BaseRoomInfoDao baseRoomInfoDao;
	
	private StandardSinkParamDao standardSinkParamDao;
	private List standSinkList;
	
	private Map tjParam = new HashMap();
	
	public void prepare() throws Exception {

	}
	
	public BaseBuildingInfoAction() throws Exception{
		model = new BaseBuildingInfo();
		selectBean = new BaseBuildingInfoQuerySpec();
	}
	
	public void setBaseBuildingInfoDao(BaseBuildingInfoDao baseBuildingInfoDao) {
		this.baseBuildingInfoDao = baseBuildingInfoDao;
	}

	public String execute(){
		return this.list();
	}
	
	public String list(){
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		selectBean.queryParamString();
		list = this.baseBuildingInfoDao.selectEntityPaginatedList(new BaseBuildingInfo(),selectBean);
		return "list";
	}
	
	public String add(){
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		standSinkList = this.standardSinkParamDao.selectEntityList(new StandardSinkParam(), new StandardSinkParamQuerySpec());
		return "edit";
	}
	
	public String save(){
		BaseBuildingInfo entity = (BaseBuildingInfo)model;
		SysAccessary sysAccessary = new SysAccessary();
		Long link_id = null;
		if(entity.getId()==null){
			link_id = baseBuildingInfoDao.insertEntity(entity);
		}else{
			baseBuildingInfoDao.updateEntity(entity);
			link_id = entity.getId();
		}
		
		if(getUploadFileList().size()>0){//说明本次操作有新文件上传，此时就先把旧文件删除，再添加新文件
			this.sysAccessaryDao.deleteAccByLinkId(link_id);
		}
		for (UploadFile uploadFile : getUploadFileList()) {
			sysAccessary = new SysAccessary();
			sysAccessary.setLink_id(link_id);
			sysAccessary.setLink_tab("BASE_BUILDING_INFO");
			sysAccessary.setAc_sname(uploadFile.getFileName());
			sysAccessary.setUrl(uploadFile.getFileSavePath());
			this.sysAccessaryDao.insertEntity(sysAccessary);
		}
		return "success";
	}
	
	public String edit(){
		shareAreaList = baseShareAreaInfoDao.selectEntityList(new BaseShareAreaInfo(), new BaseShareAreaInfoQuerySpec());
		model = this.baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(),id);
		
		standSinkList = this.standardSinkParamDao.selectEntityList(new StandardSinkParam(), new StandardSinkParamQuerySpec());
		//查询附件
		accessary = this.sysAccessaryDao.selectAccListByLinkId(id);
		return "edit";
	}

	public String view(){
		model = this.baseBuildingInfoDao.selectEntity(new BaseBuildingInfo(),id);
		accessary = this.sysAccessaryDao.selectAccListByLinkId(id);
		
		roomList = baseRoomInfoDao.selectRoomListByBuildId(id);
		tjParam = this.baseBuildingInfoDao.selectTjParam(id);
		return "view";
	}
	
	public void deleteAcc(Long link_id) throws Exception {
		SysAccessary sysAccessary = new SysAccessary();
		sysAccessary.setId(Long.valueOf(id));
		this.sysAccessaryDao.deleteEntity(sysAccessary,link_id);

	}
	
	public String delete(){
	//	因屏蔽级联删除用户信息而屏蔽此功能 20130510
//		this.baseBuildingInfoDao.deleteBuildingInfoLinkedObj(id);
		model = this.baseBuildingInfoDao.deleteEntity(new BaseBuildingInfo(),id);
		return "success";
	}
	
	public BaseBuildingInfoQuerySpec getSelectBean() {
		return selectBean;
	}

	public void setSelectBean(BaseBuildingInfoQuerySpec selectBean) {
		this.selectBean = selectBean;
	}

	public BaseShareAreaInfoDao getBaseShareAreaInfoDao() {
		return baseShareAreaInfoDao;
	}

	public void setBaseShareAreaInfoDao(BaseShareAreaInfoDao baseShareAreaInfoDao) {
		this.baseShareAreaInfoDao = baseShareAreaInfoDao;
	}

	public List getShareAreaList() {
		return shareAreaList;
	}

	public void setShareAreaList(List shareAreaList) {
		this.shareAreaList = shareAreaList;
	}

	public BaseBuildingInfoDao getBaseBuildingInfoDao() {
		return baseBuildingInfoDao;
	}

	public SysAccessaryDao getSysAccessaryDao() {
		return sysAccessaryDao;
	}

	public void setSysAccessaryDao(SysAccessaryDao sysAccessaryDao) {
		this.sysAccessaryDao = sysAccessaryDao;
	}

	public SysAccessary getAccessary() {
		return accessary;
	}

	public void setAccessary(SysAccessary accessary) {
		this.accessary = accessary;
	}

	public List getRoomList() {
		return roomList;
	}

	public void setRoomList(List roomList) {
		this.roomList = roomList;
	}

	public BaseRoomInfoDao getBaseRoomInfoDao() {
		return baseRoomInfoDao;
	}

	public void setBaseRoomInfoDao(BaseRoomInfoDao baseRoomInfoDao) {
		this.baseRoomInfoDao = baseRoomInfoDao;
	}

	public StandardSinkParamDao getStandardSinkParamDao() {
		return standardSinkParamDao;
	}

	public void setStandardSinkParamDao(StandardSinkParamDao standardSinkParamDao) {
		this.standardSinkParamDao = standardSinkParamDao;
	}

	public List getStandSinkList() {
		return standSinkList;
	}

	public void setStandSinkList(List standSinkList) {
		this.standSinkList = standSinkList;
	}

	public Map getTjParam() {
		return tjParam;
	}

	public void setTjParam(Map tjParam) {
		this.tjParam = tjParam;
	}

}
